<template>
	<view class="pages">
		<view class="mains">
			<view class="items imgs" @click="toChooseimg">
				<text>头像</text>
				<view class="right">
					<image :src="userInfo.avatar?userInfo.avatar:'/static/64.jpg'" mode="scaleToFill"></image>
					<uni-icons type="right" color="#000" size="25" />
				</view>
			</view>
			<view class="items" @click="showOpen">
				<text>用户名</text>
				<view class="right">
					<text>{{userInfo.nickname}}</text>
					<uni-icons type="right" color="#000" size="25" />
				</view>
			</view>
		</view>
		<uni-popup ref="conPopup" type="center">
			<view class="pop-box">
				<text class="titile">请输入昵称</text>
				<view class="mode-input">
					<input class="uni-input" v-model="inputs" maxlength="8" placeholder="请输入您的昵称" />
					<text>{{inputs.length}}/8</text>
				</view>
				
				<view class="bth" @click="sureName">完成</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {baseUrl} from "@/utils/config.js"
	export default {
		data() {
			return {
				nickName:this.$store.state.userInfo.nickname,
				inputs:"",
			};
		},
		computed:{
			 userInfo(){
				 return this.$store.state.userInfo
			 }
		},
		methods:{
			showOpen(){
				this.inputs=""
				this.$refs.conPopup.open();
			},
			toChooseimg(){
				uni.chooseImage({
					count:1,
					success: (chooseImageRes) => {
						const token = uni.getStorageSync('token')||""
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: baseUrl+'/app/upload',
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								'Authorization':token?'Bearer '+token:'',
							}, 
							success: async (uploadFileRes) => {
								const res = JSON.parse(uploadFileRes.data)
								if(res.code==200){
									let nowUser = this.userInfo
									this.userInfo.avatar = res.data.url
									nowUser.avatar=res.data.url
									await this.$api('/app/user/update','POST',{
										nickname:this.userInfo.nickname,
										avatar:this.userInfo.avatar
									})
									this.$store.dispatch('setUserData',nowUser)
								}
								 uni.showToast({
								 	icon:"none",
								 	title:res.msg
								 })
							}
						});
					}
				})
			},
			async sureName(){
				console.log(123132)
				if(!this.inputs)return uni.showToast({
									title: '请填写昵称',
									icon: 'none'
										});
				let{data:res} = await this.$api('/app/user/update','POST',{
					nickname:this.inputs,
					avatar:this.userInfo.avatar
				})
				if(res.code==200){
					let nowUser = this.userInfo
					nowUser.nickname=this.inputs
					this.$store.dispatch('setUserData',nowUser)
					 uni.showToast({
					 	icon:"none",
					 	title:res.msg
					 })
					 this.$refs.conPopup.close()
				}else{
					uni.showToast({
						icon:"none",
						title:res.msg
					})
				}						
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #F8F8F8;
	}
	.pop-box{
		width: 90vw;
		border-radius: 30rpx;
		padding: 30rpx;
		display: flex;
		flex-direction: column;
		background-color: #F8F8F8;
		gap: 30rpx;
		.titile{
			font-weight: bold;
			font-size: 40rpx;
		}
		.bth{
			width: 80%;
			margin: 0 auto;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-size: 40rpx;
			font-weight: bold;
			background-color: #529AEE;
			margin-top: 40rpx;
			color: #fff;
			border-radius: 60rpx;
		}
		.mode-input{
			width: 100%;
			background-color: #fff;
			display: flex;
			align-items: center;
			height: 80rpx;
			.uni-input{
				flex: 1;
				height: 80rpx;
				text-indent: 30rpx;
			}
			text{
				padding-right: 20rpx;
				color: rgba(74,137,220, 0.8);
			}
		}
	}
.pages{
	padding: 20rpx 30rpx;
	
	.mains{
		background-color: #fff;
		border-radius: 20rpx;

		.items{
		  background-color: #fff;
		  width: 100%;
		  display: flex;
		  align-items: center;
		  justify-content: space-between;
		  font-size: 35rpx;
		  color: #333;
		  font-weight: 400;
		  padding:30rpx 20rpx;
		  .right{
			  display: flex;
			  align-items: center;
			  image{
				  width: 80rpx;
				  height: 80rpx;
				  border-radius: 50%;
			  }
		  }
		}
		.imgs{
			padding: 40rpx 20rpx;
			border-bottom: 1rpx solid #F1F1F1;
		}
	}
}
</style>
